package com.diamondfsd.jooq.methods;

import com.diamondfsd.jooq.learn.codegen.tables.records.S1UserRecord;
import org.jooq.DSLContext;

import static com.diamondfsd.jooq.learn.codegen.Tables.S1_USER;

public class UpdateMethod {

  public static void updateByRecord(DSLContext dslContext) {
//    Record 方式默认通过主键进行作为update语句的where条件
    S1UserRecord record = dslContext.newRecord(S1_USER);
    record.setId(1);
    record.setUsername("usernameUpdate-2");
    record.setAddress("record-address-2");
    record.update();
    // 生成SQL:  update `learn-jooq`.`s1_user` set `learn-jooq`.`s1_user`.`id` = 1, `learn-jooq`.`s1_user`.`username` = 'usernameUpdate-2', `learn-jooq`.`s1_user`.`address` = 'record-address-2' where `learn-jooq`.`s1_user`.`id` = 1

    S1UserRecord record2 = dslContext.newRecord(S1_USER);
    record2.setUsername("usernameUpdate-noID");
    record2.update();
    // 生成SQL: update `learn-jooq`.`s1_user` set `learn-jooq`.`s1_user`.`username` = 'usernameUpdate-noID' where `learn-jooq`.`s1_user`.`id` is null
  }

  public static void updateByDSL(DSLContext dslContext) {
    dslContext.update(S1_USER)
        .set(S1_USER.USERNAME, "apiUsername-1")
        .set(S1_USER.ADDRESS, "update-address")
        .where(S1_USER.ID.eq(1))
        .execute();
  }
}
